package com.zy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zy.entity.Customer;
import com.zy.entity.CustomerEx;
import com.zy.entity.Dict;
import com.zy.entity.User;
import com.zy.service.CustomerService;
import com.zy.service.LinkManService;
import com.zy.service.impl.CustomerServiceImpl;
import com.zy.service.impl.LinkManServiceImpl;

/**
 * Servlet implementation class CustServlet
 */
@WebServlet("/CustServlet")
public class CustServlet extends HttpServlet {
	CustomerService cs=new CustomerServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String control = request.getParameter("control");
		
		if (control.equals("toDynamicQuery")) {
			//查询级别
			List<Dict> dict = cs.getDict();
			//存域
			request.setAttribute("list", dict);
			request.getRequestDispatcher("/WEB-INF/customer/select.jsp").forward(request, response);
		}
		if (control.equals("dynamicQuery")) {
			String custname = request.getParameter("custname");
			int custlevel = Integer.parseInt(request.getParameter("custlevel"));
			String custsource = request.getParameter("custsource");
			List<Customer> customers = new ArrayList<>();
			if (custname.length()!=0&&custsource.equals("")) {
				customers = cs.getCustomernl(custname, custlevel);
			}
			if (custname.equals("")&&custsource.length()!=0) {
				customers = cs.getCustomerls(custlevel, custsource);
			}
			if (custname.length()!=0&&custsource.length()!=0) {
				System.out.println("都不为空");
				customers = cs.getCustomernls(custname, custlevel, custsource);
			}
			if (custname.equals("")&&custsource.equals("")) {
				customers = cs.getCustomernll(custlevel);
				
			}
			request.setAttribute("list", customers);
			request.getRequestDispatcher("/WEB-INF/customer/list.jsp").forward(request, response);
		}
		if (control.equals("statistics")) {
			List<CustomerEx> customerExs = new ArrayList<>();
			customerExs = cs.getCS();
			request.setAttribute("list", customerExs);
			request.getRequestDispatcher("/WEB-INF/customer/sourceCount.jsp").forward(request, response);
		}
		if (control.equals("statisticsLevel")) {
			List<CustomerEx> customerExs = new ArrayList<>();
			customerExs = cs.getCP();
			request.setAttribute("list", customerExs);
			request.getRequestDispatcher("/WEB-INF/customer/levelCount.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
